package com.ag.lwc.service.impl;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlbeans.XmlObject;

import com.ag.bean.TbContactLog;
import com.ag.bean.TbContactWay;
import com.ag.dao.TbContactLogDAO;
import com.ag.dao.TbContactWayDAO;
import com.ag.lwc.service.SwitchService;
import com.ag.zhh.service.ContactLogService;
import com.hnisi.sms.service.SmsServiceClient;
import com.hnisi.sms.service.SmsServicePortType;

public class ContactLogServiceImpl implements ContactLogService {
	private static final Log log = LogFactory
			.getLog(ContactLogServiceImpl.class);

	private TbContactLogDAO contactLogDAO;
	private TbContactWayDAO contactWayDAO;
	private SwitchService switchservice;
	private static SmsServiceClient client;
	private static SmsServicePortType service;

	static {
		client = new SmsServiceClient();
		service = client.getSmsServiceHttpPort();
	}

	/**
	 * 通话日志
	 * 
	 * @param ctBeginTime
	 *            开始时间
	 * @param ctFinishTime
	 *            结束时间
	 * @param ctCaller
	 *            主叫
	 * @param ctCalled
	 *            被叫
	 * @param ctAudioUrl
	 *            录音地址
	 */
	@Override
	public boolean addPhoneContactLog(Date ctBeginTime, Date ctFinishTime,
			String ctCaller, String ctCalled, String ctAudioUrl) {
		TbContactWay contactWay = contactWayDAO.findById("0001");

		TbContactLog contactLog = new TbContactLog();
		contactLog.setCtBeginTime(ctBeginTime);
		contactLog.setCtFinishTime(ctFinishTime);
		contactLog.setCtCaller(ctCaller);
		contactLog.setCtCalled(ctCalled);
		contactLog.setCtAudioUrl(ctAudioUrl);
		contactLog.setTbContactWay(contactWay);
		contactLogDAO.save(contactLog);
		return true;
	}

	public void setContactLogDAO(TbContactLogDAO contactLogDAO) {
		this.contactLogDAO = contactLogDAO;
	}

	public void setContactWayDAO(TbContactWayDAO contactWayDAO) {
		this.contactWayDAO = contactWayDAO;
	}

	public SwitchService getSwitchservice() {
		return switchservice;
	}

	public void setSwitchservice(SwitchService switchservice) {
		this.switchservice = switchservice;
	}

	/**
	 * 群发短信通讯日志，
	 * 
	 * @param ctCaller
	 *            发送人姓名
	 * @param ctCalled
	 *            接收人姓名
	 * @param ctCalledUId
	 *            接收人用户ID
	 * @param ctCalledNumber
	 *            接收人手机号码
	 * @param ctSmsContent
	 *            短信内容
	 * @param eventId
	 *            当前事件ID
	 */
	@Override
	public boolean addMessageContactLogByEvent(final String ctCaller,
			final String[] ctCalleds, final String[] ctCalledUIds,
			final String[] ctCalledNumbers, final String ctSmsContent, final String eventId) {
		Thread thread = new Thread(){
			public void run(){
				 TbContactWay contactWay = contactWayDAO.findById("0002");
					// contactLog.setCtBeginTime(ctBeginTime);
					Date finishDate = new Date();

					for (int i = 0; i < ctCalleds.length; i++) {
						TbContactLog contactLog = new TbContactLog();
						contactLog.setCtFinishTime(finishDate);
						contactLog.setCtCaller(ctCaller);
						contactLog.setCtCalled(ctCalleds[i]);
						contactLog.setCtSmsContent(ctSmsContent);
						contactLog.setTbContactWay(contactWay);
						contactLog.setA(ctCalledUIds[i]);
						contactLog.setB(ctCalledNumbers[i]);
						contactLog.setC(eventId);
						contactLogDAO.save(contactLog);
						if (ctCalledNumbers[i] != null && !"".equals(ctCalledNumbers[i])) {
							int flag = switchservice.querySwitchStatus(1);
							if(flag==1)
							{
								try {
									XmlObject xmlObject = service.sendMT2("yjzh", "11111111",
											"yjzh", ctSmsContent, "", false, "",
											ctCalledNumbers[i], true);
									log.info("群发短信接口回执XML----" + xmlObject.toString());
								} catch (Exception e) {
									log.error("网络不通，请检查网络！");
									e.printStackTrace();
								}
							}
			
						}
					}

//					if (ctCalleds == null || ctCalleds.length < 1) {
//						return false;
//					}
//					if (ctCalleds.length != ctCalledUIds.length
//							|| ctCalledUIds.length != ctCalledNumbers.length) {
//						return false;
//					}
			}
			
		};
	 thread.start();
		return true;
	}

	/**
	 * 开线程发短信
	 */
	@Override
	public boolean addOneMessageContactLogByEvent(String ctCaller,String ctCalled, String ctCalledUId, final String ctCalledNumber,final String ctSmsContent, String eventId)
	{
		TbContactWay contactWay = contactWayDAO.findById("0002");
		Date finishDate = new Date();
		TbContactLog contactLog = new TbContactLog();
		contactLog.setCtFinishTime(finishDate);
		contactLog.setCtCaller(ctCaller);
		contactLog.setCtCalled(ctCalled);
		contactLog.setCtSmsContent(ctSmsContent);
		contactLog.setTbContactWay(contactWay);
		contactLog.setA(ctCalledUId);
		contactLog.setB(ctCalledNumber);
		contactLog.setC(eventId);
		contactLogDAO.save(contactLog);
		
		/**
		 * 开进程发送短信
		 */
		Thread thread = new Thread(){
			
			public void run()
			{
				System.out.println("开进程发送短信: 短信接收方的号码："+ctCalledNumber);
				try {
					XmlObject xmlObject = service.sendMT2("yjzh", "11111111", "yjzh",
							ctSmsContent, "", false, "", ctCalledNumber, true);
					log.info("单发短信接口回执XML----" + xmlObject.toString());
				} catch (Exception e) {
					log.error("网络不通，请检查网络！");
					e.printStackTrace();
				}
			}
		};
		int flag = switchservice.querySwitchStatus(1);
		if(flag==1) thread.start();
		
		return true;
	}
	
	/**
	 * 单发短信通讯日志，
	 * 
	 * @param ctCaller
	 *            发送人姓名
	 * @param ctCalled
	 *            接收人姓名
	 * @param ctCalledUId
	 *            接收人用户ID
	 * @param ctCalledNumber
	 *            接收人手机号码
	 * @param ctSmsContent
	 *            短信内容
	 * @param eventId
	 *            当前事件ID
	 */

	public boolean addOneMessageContactLogByEvent2(final String ctCaller,final String ctCalled, final String ctCalledUId, final String ctCalledNumber,final String ctSmsContent, final String eventId)
	{
		Thread thread = new Thread(){
			public void run(){
				TbContactWay contactWay = contactWayDAO.findById("0002");

				//contactLog.setCtBeginTime(ctBeginTime);
				Date finishDate = new Date();

				TbContactLog contactLog = new TbContactLog();
				contactLog.setCtFinishTime(finishDate);
				contactLog.setCtCaller(ctCaller);
				contactLog.setCtCalled(ctCalled);
				contactLog.setCtSmsContent(ctSmsContent);
				contactLog.setTbContactWay(contactWay);
				contactLog.setA(ctCalledUId);
				contactLog.setB(ctCalledNumber);
				contactLog.setC(eventId);
				contactLogDAO.save(contactLog);
//				if (ctCalledNumber == null) {
//					return false;
//				}
				int flag = switchservice.querySwitchStatus(1);
				if(flag==1)
				{
					try {
						XmlObject xmlObject = service.sendMT2("yjzh", "11111111", "yjzh",
								ctSmsContent, "", false, "", ctCalledNumber, true);
						log.info("单发短信接口回执XML----" + xmlObject.toString());
					} catch (Exception e) {
						log.error("网络不通，请检查网络！");
						e.printStackTrace();
					}
				}
		
			}
		};
		thread.start();
		return true;
	}


	
	
	/**
	 * 增加短信通信日志
	 * 
	 * @param ctCaller
	 *            发送人名
	 * @param ctCalled
	 *            接收人名
	 * @param ctCalledNumber
	 *            接收人号码
	 * @param ctSmsContent
	 *            短信内容
	 */
	@Override
	@Deprecated
	public void addMessageContactLog(String ctCaller, String[] ctCalleds,
			String[] ctCalledNumber, String ctSmsContent) {

		TbContactWay contactWay = contactWayDAO.findById("0002");

		// contactLog.setCtBeginTime(ctBeginTime);
		Date finishDate = new Date();
		if (ctCalleds == null || ctCalleds.length < 1) {
			return;
		}
		for (String ctCalled : ctCalleds) {
			TbContactLog contactLog = new TbContactLog();
			contactLog.setCtFinishTime(finishDate);
			contactLog.setCtCaller(ctCaller);
			contactLog.setCtCalled(ctCalled);
			contactLog.setCtSmsContent(ctSmsContent);
			contactLog.setTbContactWay(contactWay);
			contactLogDAO.save(contactLog);
		}

	}
	/**
	 * 增加短信通信日志
	 * 
	 * @param ctCaller
	 *            发送人名
	 * @param ctCalled
	 *            接收人名
	 * @param ctCalledNumber
	 *            接收人号码
	 * @param ctSmsContent
	 *            短信内容
	 */
	@Override
	@Deprecated
	public void addMessageContactLog(final String ctCaller, final String ctCalled,
			String ctCalledNumber, final String ctSmsContent) {
		if (ctCalled == null || ctCalled.equals("")) {
			return;
		}
		//创建一个新的线程
		Thread thread = new Thread(){		
			public void run(){		
				TbContactWay contactWay = contactWayDAO.findById("0002");
				Date finishDate = new Date();
				TbContactLog contactLog = new TbContactLog();
				contactLog.setCtFinishTime(finishDate);
				contactLog.setCtCaller(ctCaller);
				contactLog.setCtCalled(ctCalled);
				contactLog.setCtSmsContent(ctSmsContent);
				contactLog.setTbContactWay(contactWay);
				contactLogDAO.save(contactLog);
			}
		};
		thread.start();

		
	}
	/**
	 * 传真发送日志
	 * 
	 * @param ctBeginTime
	 *            传真发送时间
	 * @param ctFinishTime
	 *            传真结束时间
	 * @param ctCaller
	 *            发送人
	 * @param ctCalled
	 *            接收人
	 * @param ctFaxFile
	 */
	@Override
	public boolean addFaxContactLog(Date ctBeginTime, Date ctFinishTime,
			String ctCaller, String ctCalled, String ctFaxFile) {
		TbContactWay contactWay = contactWayDAO.findById("0003");
		TbContactLog contactLog = new TbContactLog();
		contactLog.setCtBeginTime(ctBeginTime);
		contactLog.setCtFinishTime(ctFinishTime);
		contactLog.setCtCaller(ctCaller);
		contactLog.setCtCalled(ctCalled);
		contactLog.setCtFaxFile(ctFaxFile);
		contactLog.setTbContactWay(contactWay);
		contactLogDAO.save(contactLog);
		return true;
	}

}
